package com.lv.service.room.address

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Database
import androidx.room.Delete
import androidx.room.Entity
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.Update

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * App的公共业务基础数据类
 */

//3、database
@Database(entities = [CurrentAddress::class], version = 1, exportSchema = false)
abstract class AddressCurrentDatabase : RoomDatabase() {

    abstract val addressDao: CurrentAddressDao

    companion object {

        private const val CNIAO_DB_NAME = "syy_current_address_db_1"

        @Volatile
        private var instance: AddressCurrentDatabase? = null

        @Synchronized
        fun getInstance(context: Context): AddressCurrentDatabase {
            return instance ?: Room.databaseBuilder(
                context,
                AddressCurrentDatabase::class.java,
                CNIAO_DB_NAME
            ).build().also { instance = it }
        }
    }
}

//1、entity的定义

@Entity(tableName = "current_address_db_1")
data class CurrentAddress(
    @PrimaryKey
    var _id: Int,//主键
    var forshort: String?,
    var province: String?,
    var city: String?,
    var area: String?,
    var time: Long,
    var latitude: String?="",
    var longitude: String?="",


    )
////1、entity的定义
//
//@Entity(tableName = "tb_cniao_user")
//data class CniaoUserInfo(
//    @PrimaryKey
//    val id: Int,//主键
//    val course_permission: Boolean,
//    val token: String?,
//    @Embedded
//    val user: User?
//) {
//    @Keep
//    data class User(
//        @ColumnInfo(name = "cniao_user_id")
//        val id: Int,//用户id
//        val logo_url: String?,//用户头像
//        val reg_time: String?,//用户注册时间
//        val username: String?//用户名
//    )
//}

//2、dao层的定义
@Dao
interface CurrentAddressDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAddress(address: CurrentAddress)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    fun updateAddress(address: CurrentAddress)

    @Delete
    fun deleteAddress(address: CurrentAddress)

    // = , in , like
    @Query("select * from current_address_db_1 where _id=:id")
    fun queryLiveAddress(id: Int = 0): LiveData<CurrentAddress>

    @Query("select * from current_address_db_1 where _id=:id")
    suspend fun queryAddress(id: Int = 0): CurrentAddress?

}



